﻿using Apps.Common;
using Apps.Common.ApiVersion;
using Apps.Extensions.Mapping;
using Apps.IService;
using Apps.Model.Models;
using Apps.Model.ViewModels;
using Apps.Repository.UnitOfWork;
using Apps.WebApi.Filter;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace Apps.WebApi.Controllers
{
    [ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ApiVersion.v1))]
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        private readonly ISysUserService _userService;
        private readonly IUnitOfWork _unitOfWork;
        private IMapper _mapper;
        public UserController(ISysUserService userService, IUnitOfWork unitOfWork, IMapper mapper)
        {
            _userService = userService;
            _unitOfWork = unitOfWork;
            _mapper = mapper;
        }

        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        // GET: api/<UserController>
        [HttpGet]
        //[Authorize]
        [ServiceFilter(typeof(CustomActionFilter))]
        public async Task<ApiResponse<List<UserDto>>> Get()
        {
            var data = (await _userService.GetAllAsync()).To<UserDto>(_mapper).ToList();
            return ApiResponse<List<UserDto>>.Success(data);
        }

        // GET api/<UserController>/5
        [HttpGet("{id}")]
        public async Task<UserDto> Get(string username)
        {
            return await _userService.GetUserDtoByNameAsync(username);
        }

        // POST api/<UserController>
        [HttpPost]

        public async Task Post([FromBody] SysUser user)
        {
            await _userService.AddAsync(user);
        }

        [HttpGet("page")]
        public async Task<PagedResult<SysUser>> Post(int pageIndex, int pageSize)
        {
            var entity = await _userService.GetPagedAsync(pageIndex, pageSize);
            return entity;
        }
    }
}